package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.AdminDAO;
import com.dao.AdminRoleDAO;
import com.dao.RoleDAO;
import com.pojo.Admin;
import com.pojo.AdminRole;
import com.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@Service
public class AdminRoleService extends ServiceImpl<AdminRoleDAO, AdminRole> {
    @Autowired
    private AdminRoleDAO adminRoleMapper;

    @Autowired
    private RoleDAO roleDAO;

    @Autowired
    private AdminDAO adminDAO ;

    /**
     * 根据用户ID查用户的所有角色
     * @param id
     * @return
     */
    public List<String> shifouGuanliyuan(String id){
        return adminRoleMapper.shifouGuanliyuan(id);
    }

    public IPage findRoleByAdmin(int page, int size, String text) {
        IPage iPage = new Page(page, size);
        IPage<Map> maps = adminRoleMapper.findRoleByAdmin(iPage, text);
        return maps;
    }

    public Map findAllRole(String adminId) {
        Map map = new HashMap();
        List all = roleDAO.getNotRolesByAdmin(adminId);//所有可用的角色

        List<Role> adminRole = roleDAO.getRolesByAdmin(adminId);

        map.put("all",all);
        map.put("admin",adminRole);

        return map;

    }

    /**
     * 根据角色查询管理员
     * @param role
     * @return
     */
    public List<Admin> findAdminsByRole(String role) {
        QueryWrapper<Role> query = new QueryWrapper() ;
        String[] arr = role.split(",") ;

        for(String str : arr) {
            query.eq("role_name" , str) ;
        }

        List<Role> rols = roleDAO.selectList(query) ;
        Set<Admin> res = new HashSet<>() ;
        for(Role r : rols) {
            List<AdminRole> admins = adminRoleMapper.selectList(new QueryWrapper<AdminRole>().in("role_id" , r.getRoleId())) ;
            for(AdminRole admin : admins) {
                Admin adm = adminDAO.selectById(admin.getAdminId()) ;
                if(adm != null) {
                    res.add(adm) ;
                }
            }
        }
        return new ArrayList<>(res) ;
    }

    public Map adminRoleSave(String adminId, String roleId) {
        AdminRole adminRole = new AdminRole();
        //int adminIdInt = Long.parseInt(adminId);
        //int roleIdInt = Integer.parseInt(roleId);
        adminRole.setAdminId(adminId);
        adminRole.setRoleId(roleId);
        int n = adminRoleMapper.insert(adminRole);
        Map map = new HashMap();
        map.put("n", n);
        map.put("id", adminRole.getAdminRoleId());
        return map;
    }

    public int adminRoleDel(String adminId,String adminRoleId) {
        QueryWrapper q = new QueryWrapper();
        q.eq("admin_id",adminId);
        q.eq("role_id",adminRoleId);
        int n = adminRoleMapper.delete(q);
        return n;
    }
}
